zip
¶names = ['John', 'Juan', 'João', 'Giovanni']
ages = [23, 18, 24, 22]
zip(names, ages)
<zip at 0x11bdebec0>
names = ['John', 'Juan', 'João', 'Giovanni']
ages = [23, 18, 24, 22]
list(zip(names, ages))
[('John', 23), ('Juan', 18), ('João', 24), ('Giovanni', 22)]
names = ['John', 'Juan', 'João', 'Giovanni']
ages = [23, 18, 24, 22]
for name, age in zip(names, ages):
print(f'{name} is {age} years old')
John is 23 years old Juan is 18 years old João is 24 years old Giovanni is 22 years old
names = ['John', 'Juan', 'João', 'Giovanni']
ages = [23, 18, 24, 22]
majors = ['Chemistry', 'Animation', 'Sociology', 'Secondary Education']
for name, age, major in zip(names, ages, majors):
print(f'{name} is {age} years old and studies {major}')
John is 23 years old and studies Chemistry Juan is 18 years old and studies Animation João is 24 years old and studies Sociology Giovanni is 22 years old and studies Secondary Education
fruits = ['apple', 'pear', 'blueberry', 'grape', 'strawberry']
plant_types = ['tree', 'tree', 'bush', 'vine']
for fruit, plant_type in zip(fruits, plant_types):
print(f'The {fruit} grows on a {plant_type}.')
The apple grows on a tree. The pear grows on a tree. The blueberry grows on a bush. The grape grows on a vine.
word1 = 'planter'
word2 = 'started'
for letter1, letter2 in zip(word1, word2):
if letter1 == letter2:
print(f'{letter1} == {letter2} ✅')
else:
print(f'{letter1} != {letter2}')
p != s l != t a == a ✅ n != r t == t ✅ e == e ✅ r != d
enumerate
¶enumerate(['Paula', 'Patty', 'Peter', 'Peregrin'])
<enumerate at 0x11be32e40>
list(enumerate(['Paula', 'Patty', 'Peter', 'Peregrin']))
[(0, 'Paula'), (1, 'Patty'), (2, 'Peter'), (3, 'Peregrin')]
for index, name in enumerate(['Paula', 'Patty', 'Peter', 'Peregrin']):
print(f'{index}: {name}')
0: Paula 1: Patty 2: Peter 3: Peregrin
zip
enumerate